package cn.xuziao.databasecourse.tools;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuziao
 * @date 2021/11/3 23:00
 */
@Service
public class InitData {
    public ArrayList<String> getUrls (String keyPrefix) {
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tKN2dcG6AqJd3tnbCoP";
        String accessKeySecret = "KKEaCpM6Bb9kLWZq6HsJe9cXzKjhso";
        String bucketName = "xza-bucket";
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ArrayList<String> urls = new ArrayList<>();
        final int maxKeys = 1000;
// 列举文件。
        ObjectListing objectListing = ossClient.listObjects(new ListObjectsRequest(bucketName)
                .withMaxKeys(maxKeys)
                .withPrefix(keyPrefix));
        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        int i = 0;
        for (OSSObjectSummary s : sums) {
            urls.add("https://oss.xuziao.cn/"+s.getKey());
            i++;
        }
        ossClient.shutdown();

        return urls;
    }
}
